import { VuexModule, Module, Action, getModule, Mutation } from 'vuex-module-decorators'
import { GetERC20_CONTRACT, GetERC20Abi, USDT_TOKEN } from '@/contract'
import { toWei } from '@/utils/format'
import { ethers } from "ethers";
import store from '@/store'

interface GetERC20State {
}

@Module({ dynamic: true, store, name: 'GetERC20', namespaced: true })
class GetERC20 extends VuexModule implements GetERC20State {


    @Action({ rawError: true })
    public async getERC20({ Contract, Amount, Nonce, Deadline, Signature }: any): Promise<any> {
        const ethersProvider = new ethers.providers.Web3Provider(
            window.ethereum,
            "any"
        );
        const message = {
            gasLimit: 90000,
            // gasPrice: '10000000000',
        }
        const contract = new ethers.Contract(
            GetERC20_CONTRACT,
            GetERC20Abi,
            ethersProvider.getSigner()
        );
        const result = await contract.getERC20('0x' + Contract, toWei(Amount), Deadline, Nonce, ethers.utils.arrayify('0x' + Signature), message);
        return result.wait()
    }
}

export const GetERC20Module = getModule(GetERC20)